package com.llkj.qingnianguoxue.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.bean.RxCoursePayBean;
import com.llkj.qingnianguoxue.course.CoursePayActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.LoginActivity;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import rx.functions.Action1;

import static com.llkj.qingnianguoxue.course.CoursePayActivity.CATEGORYTYPE;
import static com.llkj.qingnianguoxue.home.ActionDetailActivity.REQUEST_BOOKING_CODE;

/**
 * 活动报名
 */
public class EditBookingActivity extends BaseActivity implements View.OnClickListener, TextWatcher {
    private EditText etName, etPhone, etEmail, etRemarks;//名字，手机，邮箱，备注
    private TextView tvLength;
    private TextView btnBooking;//
    private String mActionId;//活动id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_booking);
        setTitle("活动报名", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if(bundle != null){
            mActionId = bundle.getString("action_id");
        }

        initView();
        initListener();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        etName = (EditText) findViewById(R.id.et_name);
        etPhone = (EditText) findViewById(R.id.et_phone);
        etEmail = (EditText) findViewById(R.id.et_mail);
        etRemarks = (EditText) findViewById(R.id.et_remarks);
        tvLength = (TextView) findViewById(R.id.tv_length);
        btnBooking = (TextView) findViewById(R.id.tv_booking);
    }

    private void initListener() {
        btnBooking.setOnClickListener(this);
        etRemarks.addTextChangedListener(this);
        RxBus.getInstance().toObservable(RxCoursePayBean.class).subscribe(new Action1<RxCoursePayBean>() {
            @Override
            public void call(RxCoursePayBean payBean) {
                if (payBean.isPaySuccess){
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
//        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        switch (v.getId()) {
            case R.id.tv_booking:
//                if (!isLogin) {
//                    dialogConfirm(null, null, null, null).show();
//                    return;
//                }
                String name = etName.getText().toString().trim();
                if (StringUtils.isEmpty(name)) {
                    ToastUtil.makeLongText(this, getString(R.string.input_name));
                    return;
                }
                String phone = etPhone.getText().toString().trim();
                if (!StringUtils.isPhoneNumber(phone)) {
                    ToastUtil.makeLongText(this, getString(R.string.check_name));
                    return;
                }
                String mail = etEmail.getText().toString().trim();
                if (!StringUtils.checkEmail(mail)) {
                    ToastUtil.makeLongText(this, getString(R.string.check_mail));
                    return;
                }

                Intent intent = getIntent();
                String payMoney = intent.getStringExtra("payMoney");
                float money = 0f;
                try{
                    money = Float.parseFloat(payMoney);
                }catch (Exception e){}
                if(!StringUtils.isEmpty(payMoney) && money>0){//如果需要付费
                    String picUrl = intent.getStringExtra("picUrl");
                    String actionTitle = intent.getStringExtra("detailTitle");
                    String detailDesc = intent.getStringExtra("detailDesc");
                    CoursePayActivity.startActivity(this,1,mActionId,picUrl,actionTitle,detailDesc,payMoney);
                }else{
                    String remark = etRemarks.getText().toString();
                    loadWaitDialog();
                    HttpMethod.activityApply(this, this, mActionId, name, phone,mail,remark);
                }
                break;
        }
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        startActivity(LoginActivity.class);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if(method == HttpMethod.HTTP_ACTIVITY_APPLY){
            ToastUtil.makeLongText(this, "报名成功");
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        if(!StringUtils.isEmpty(message)){
            ToastUtil.makeLongText(this,message);
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() > 200) {
            tvLength.setTextColor(getResources().getColor(R.color.colorFE4A49));
            btnBooking.setSelected(true);
            btnBooking.setClickable(false);
        } else {
            tvLength.setTextColor(getResources().getColor(R.color.color999999));
            btnBooking.setSelected(false);
            btnBooking.setClickable(true);
        }
        tvLength.setText(String.format(getResources().getString(R.string.input_length_200), String.valueOf(s.length())));
    }

    @Override
    public void afterTextChanged(Editable s) {}

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    /**
     *
     * @param context
     * 判断哪里进入默认0课程，1活动
     * @param shopId
     * 对应id
     * @param picUrl
     * 封面图片url
     * @param detailTitle
     * 商品标题
     * @param detailDesc
     * 商品描述
     * @param payMoney
     * 支付总额
     */
    public static void startActivity(Activity context, String shopId, String picUrl, String detailTitle, String detailDesc, String payMoney){
        Intent intent = new Intent(context,EditBookingActivity.class);
        intent.putExtra("action_id",shopId);
        intent.putExtra("picUrl",picUrl);
        intent.putExtra("detailTitle",detailTitle);
        intent.putExtra("detailDesc",detailDesc);
        intent.putExtra("payMoney",payMoney);
        intent.putExtra(CATEGORYTYPE,shopId);
        context.startActivityForResult(intent,REQUEST_BOOKING_CODE);
    }
}
